###############################################################################
######################Figure 1: Polarization Figure############################
###############################################################################

#install.packages('ggplot2')
library(ggplot2)
#install.packages('dplyr')
library(dplyr)
#install.packages('ggrepel')
library(ggrepel)

#####Figure 1#####

#Uk in 1997 and UK 2017#
#Load Data sets to combine

ches_2017 <- import("CHES Data/CHES_means_2017 (2).dta") %>%
  filter(country == 11) %>%
  select(country, party, vote, lrecon)

ches_2017$country <- '2017'

ches <- import("CHES Data/1999-2014_CHES_dataset_means-3.dta") %>%
  filter(country == 11 & electionyear == 1997) %>%
  select(country, party, vote, lrecon)

ches$country <- '1997'

ches <- rbind(ches, ches_2017) #combine the datasets

#make the plot
ggplot(ches, aes(x = lrecon, y = country, label = party)) + 
  geom_point(aes(size = vote))+
  geom_text_repel(box.padding   = 0.35, 
                  point.padding = 0.5,
                  segment.color = 'grey50',
                  label.size = NA) +
  theme_bw()+
  labs(x = 'Economic Position', y = '', size = "Vote Share (%)")+
  xlim(0,10)

#ggsave("figure_1.pdf", width = 10 , height = 4, units = "in")




#####Figure 2######

#Read in Party Data#

CHES <- readRDS('CHES_new')

#Filter to only include the three relevant elections#

figure2.dat <- CHES[(CHES$country == "hun" & CHES$electionyear == 2014)| (CHES$country == "aus" & CHES$electionyear == 2006) |  (CHES$country == "cz" &CHES$electionyear == 2010) ,]

#Adjust the levels of the country factors for plotting#

figure2.dat$country <- factor(figure1.dat$country, levels = c("hun", "aus", "cz"))

#Plot the parties in each election#

ggplot(figure2.dat[-5,], aes(x = lrecon, y = country)) +
  geom_point(aes(size = vote)) +
  geom_label_repel(aes(label = party),
                   box.padding   = 0.35, 
                   point.padding = 0.5,
                   segment.color = 'grey50',
                   label.size = NA)+
  xlim(c(0,10)) +
  theme_bw() +
  scale_size_continuous(name = "Vote Share (%)") +
  labs(title = "Distributions of Party Economic Positions in the Czech Republic (2013), Austria (2006), and Hungary (2014)", x = "Left-Right Economic Position", y = "Country")  +
  scale_y_discrete(labels = c("Hungary", "Austria", "Czech Republic"))

#Save the Plot#

#ggsave("figure_2.pdf", width = 10 , height = 4, units = "in")
